<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="role in list"
        :key="role.id"
        :label="role.id"
      >{{ role.name }}
      </el-checkbox>
      <div style="margin-top: 20px; text-align: right">
        <el-button type="primary" @click="hSave">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </el-checkbox-group>
  </div>
</template>
<script>
import { getRoles } from '../../api/setting'
import { getUserDetailById } from '../../api/user'
import { assignRoles } from '../../api/employees'
export default {
  props: {
    curId: { type: String, default: '' }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    closeDialog() {
      this.$emit('cancel')
    },
    // 获取数据
    async loadRoles() {
      try {
        const res = await getRoles(1, 100)
        this.list = res.data.rows
        console.log(res)
        // 保存数据
        this.list = res.data.rows
      } catch (err) {
        console.log(err)
      }
    },
    async loadUserDetailById() {
      try {
        const res = await getUserDetailById(this.curId)
        console.log('getUserDetailById', res)
        // 当前用户的角色数据
        this.roleIds = res.data.roleIds
      } catch (err) {
        console.log('getUserDetailById', err)
      }
    },
    // 确定保存
    async hSave() {
      try {
        const res = await assignRoles({ id: this.curId, roleIds: this.roleIds })
        console.log(res)
        this.$message.success('保存成功')
        this.$emit('cancel')
      } catch (err) {
        console.log(err)
        this.$message.error('保存失败')
      }
    }
  }
}
</script>
